/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Material;
import com.click.app.model.StorageMaterial;
import com.click.app.service.GenericManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author orgale
 */
public class StorageMaterialAction extends BaseAction {

    private GenericManager<StorageMaterial, Long> storageMaterialManager;
    private List storageMaterials;
    private StorageMaterial storageMaterial;
    private Long id;
    private String stock;
    private String limit;

    public void setId(Long id) {
        this.id = id;
    }

    public StorageMaterial getStorageMaterial() {
        return storageMaterial;
    }

    public void setStorageMaterial(StorageMaterial storageMaterial) {
        this.storageMaterial = storageMaterial;
    }

    public String delete() {
        storageMaterialManager.remove(storageMaterial.getId());
        saveMessage(getText("storageMaterial.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            storageMaterial = storageMaterialManager.get(id);
            limit = storageMaterial.getLimitFormatted();
            stock = storageMaterial.getStockFormatted();
            getSession().setAttribute("mat", storageMaterial.getMaterial());
        } else {
            storageMaterial = new StorageMaterial();
        }

        return SUCCESS;
    }
    public boolean validationPasses() {
        Float aux = 0.0f;
        boolean vp = true;
        if (getStock() == null || getStock().matches("")) {
            errorMessage("Campo requerido, stock.");
            vp = false;
        }
        if (getLimit() == null || getLimit().matches("")) {
            errorMessage("Campo requerido, limite.");
            vp = false;
        }

        try {
            if (getStock() != null && !getStock().matches("")) {
                aux = Float.parseFloat(getStock());
            }
            if (aux < 0) {
                errorMessage("Stock es negativo.");
                vp = false;
            }
        } catch (Exception e) {
            errorMessage("Stock se encuentra en formato incorrecto.");
            vp = false;
        }

        try {
            if (getLimit() != null && !getLimit().matches("")) {
                aux = Float.parseFloat(getLimit());
            }
            if (aux < 0) {
                errorMessage("Limite es negativo.");
                vp = false;
            }
        } catch (Exception e) {
            errorMessage("Limite se encuentra en formato incorrecto.");
            vp = false;
        }

        
        return vp;
    }


    public String save() throws Exception {
        storageMaterial.setMaterial((Material) getSession().getAttribute("mat"));
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (storageMaterial.getId() == null);

        if (validationPasses()){
        Double s = Double.parseDouble(getStock());
        Double l = Double.parseDouble(getLimit());

        storageMaterial.setStock(s);
        storageMaterial.setLimit(l);

        storageMaterial = storageMaterialManager.save(storageMaterial);
        }else{
            return ERROR;
        }
        String key = (isNew) ? "storageMaterial.added" : "storageMaterial.updated";
        saveMessage(getText(key));
        getSession().removeAttribute("mat");
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setStorageMaterialManager(GenericManager<StorageMaterial, Long> storageMaterialManager) {
        this.storageMaterialManager = storageMaterialManager;
    }

    public List getStorageMaterials() {
        return storageMaterials;
    }

    public String list() {
        List<StorageMaterial> warning = new ArrayList<StorageMaterial>();
        List<StorageMaterial> Awarning = new ArrayList<StorageMaterial>();
        String smWarning = "";
        String smAWarning = "";

        for (StorageMaterial sm : storageMaterialManager.getAll()) {
            if (sm.getStock() <= sm.getLimit()) {
                warning.add(sm);
                smWarning += " - " + sm.getId();
            } else if ((sm.getStock() <= (sm.getLimit() * .10))) {
                Awarning.add(sm);
                smAWarning += " - " + sm.getId();
            }
        }
        storageMaterials = storageMaterialManager.getAll();
        getRequest().setAttribute("smAllS", storageMaterialManager.getAll().size());
        getRequest().setAttribute("smAWarningS", Awarning.size());
        getRequest().setAttribute("smWarningS", warning.size());
        getRequest().setAttribute("smWarning", smWarning);
        getRequest().setAttribute("smAWarning", smAWarning);


        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    /**
     * @return the stock
     */
    public String getStock() {
        return stock;
    }

    /**
     * @param stock the stock to set
     */
    public void setStock(String stock) {
        this.stock = stock;
    }

    /**
     * @return the limit
     */
    public String getLimit() {
        return limit;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(String limit) {
        this.limit = limit;
    }
}